package com.yanq.liancar.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.PayResult;
import com.yanq.liancar.bean.SelectOrderDetailsBean;
import com.yanq.liancar.bean.SelectUserAllOrdersBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBOrderDetailsActivity extends BaseToolbarActivity {
    @BindView(R.id.item_img)
    ImageView itemImg;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.img_location)
    ImageView imgLocation;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phoneNum)
    TextView tvPhoneNum;
    @BindView(R.id.textview2)
    TextView textview2;
    @BindView(R.id.rl_address)
    RelativeLayout rlAddress;
    @BindView(R.id.dingdan)
    TextView dingdan;
    @BindView(R.id.tv_ordernum)
    TextView tvOrdernum;
    @BindView(R.id.tv_ordertime)
    TextView tvOrdertime;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_oddnum)
    TextView tvOddnum;
    @BindView(R.id.tv_evaluate)
    TextView tvEvaluate;
    @BindView(R.id.tv_price2)
    TextView tvPrice2;
    @BindView(R.id.action_bar)
    AppBarLayout actionBar;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.address_detaile)
    TextView address_detaile;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.textview1)
    TextView textview1;
    @BindView(R.id.rl_content1)
    RelativeLayout rlContent1;
    @BindView(R.id.view5)
    View view5;
    @BindView(R.id.view6)
    View view6;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**

                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。

                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    LogUtils.e("resultInfo====",resultInfo);
                    LogUtils.e("resultStatus====",resultStatus);
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Toast.makeText(SmallBOrderDetailsActivity.this,   "支付成功", Toast.LENGTH_SHORT).show();
                        RxToast.success("下单成功");
                        finish();
                    } else {

                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Toast.makeText(SmallBOrderDetailsActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }

            }

        }

    };
    private int orderid, orderStatus;
    private String order_num;
    private SelectOrderDetailsBean bean;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_orderdetails);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);
        mTvTitle.setText("订单详情");
        orderid = getIntent().getIntExtra("orderId", 0);
        orderStatus = getIntent().getIntExtra("orderStatus", 0);
        LogUtils.e("qwer",orderStatus+"=========="+orderid);
        if (orderStatus == 3) {
            tvType.setText("待收货");
            tvEvaluate.setVisibility(View.VISIBLE);
            tvEvaluate.setEnabled(true);
            tvEvaluate.setText("确认收货");
        } else if (orderStatus == 2) {
            tvType.setText("待发货");
            tvEvaluate.setVisibility(View.VISIBLE);
            tvEvaluate.setEnabled(false);
            tvEvaluate.setText("等待发货");
        } else if (orderStatus == 1) {
            tvType.setText("待支付");
            mTvRight.setVisibility(View.VISIBLE);
            tvEvaluate.setEnabled(true);
            mTvRight.setText("取消订单");
            tvEvaluate.setVisibility(View.VISIBLE);
            tvEvaluate.setText("去支付");
            mTvRight.setTextColor(R.color.main_color);
        }else if (orderStatus == 4){
            tvType.setText("待评价");
            tvEvaluate.setVisibility(View.VISIBLE);
            tvEvaluate.setEnabled(true);
            tvEvaluate.setText("去评价");
        }else {
            tvType.setText("已完成");
            tvEvaluate.setVisibility(View.VISIBLE);
            tvEvaluate.setEnabled(false);
            tvEvaluate.setText("已完成");
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(R.mipmap.delete);
        }
        initOkgo();

        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderStatus == 1) {
                    okGoDeleteOrder("llc/api/deleteOrderDetails");
                }
            }
        });

        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (orderStatus == 5) {
                    okGoDeleteOrder("llc/api/deleteOrderDetails");
                }
            }
        });
    }

    @OnClick(R.id.tv_evaluate)
    public void onViewClicked() {
        if (orderStatus == 1){
            Bundle bundle=new Bundle();
            bundle.putString("price",bean.getData().getOrder_price()+"");
            bundle.putString("goodID",bean.getData().getOrder_goodsid()+"");
            bundle.putString("addressID",bean.getData().getOrder_addressid()+"");
            bundle.putString("goodNum",bean.getData().getOrder_goods_num()+"");
            bundle.putString("goodname",bean.getData().getOrder_goods_name());
            bundle.putString("order_num",bean.getData().getOrder_num());
            RxActivityTool.skipActivity(this, SmallBPaymentActivity.class,bundle);
        }else if (orderStatus == 4){
            Bundle bundle=new Bundle();
            bundle.putInt("orderID",bean.getData().getOrderid());
            RxActivityTool.skipActivity(this, SmallBOrderEvaluateActivity.class,bundle);
        }else if (orderStatus==3){
            okGoDeleteOrder("llc/api/notarizeReceiving");
        }else if (orderStatus==5){
            //okGoDeleteOrder("llc/api/deleteOrderDetails");
        }
    }

    //查询订单详情
    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectOrderDetails")
                .tag(this)
                .params("orderid", orderid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(response.body(), SelectOrderDetailsBean.class);
                        if (bean.isIsSuccess()) {
                            RequestOptions options=new RequestOptions();
                            options.error(R.mipmap.item_img);
                            Glide.with(SmallBOrderDetailsActivity.this).load(bean.getData().getOrder_goods_photo()).apply(options).into(itemImg);
                            tvTitle.setText(bean.getData().getOrder_goods_name());
                            tvNum.setText("X" + bean.getData().getOrder_goods_num());
                            tvContent.setText(bean.getData().getOrder_goods_introduction());
                            tvPrice.setText("¥" + bean.getData().getOrder_goods_price());
                            tvName.setText(bean.getData().getOrder_recipients());
                            tvPhoneNum.setText(bean.getData().getOrder_phone());
                            address_detaile.setText(bean.getData().getOrder_shipping_address());
                            tvOrdernum.setText(bean.getData().getOrder_num());
                            tvOrdertime.setText(bean.getData().getOrder_createtime());
                            tvCompany.setText(bean.getData().getLogistics_company());
                            tvOddnum.setText(bean.getData().getTracking_num());
                            tvPrice2.setText("¥" + bean.getData().getOrder_price());
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }

    //取消订单、确认收货
    private void okGoDeleteOrder(String url) {
        OkGo.<String>post(Config.s + url)
                .tag(this)
                .params("orderid", orderid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()) {
                           RxToast.success(bean.getOutputMessage());
                           finish();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }




}
